from streamlit_javascript import st_javascript

def set_cookie(name, value, days=1):
    js = f'document.cookie = "{name}={value}; path=/; max-age={days*24*60*60}";'
    st_javascript(js)

def get_cookie(name):
    js = f"""
    const value = document.cookie
        .split('; ')
        .find(row => row.startsWith('{name}='));
    if (value) {{
        return value.split('=')[1];
    }} else {{
        return null;
    }}
    """
    return st_javascript(js)

def clear_cookie(name):
    js = f'document.cookie = "{name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";'
    st_javascript(js)
